<?php

class ectools_ctl_office extends desktop_controller{

    var $workground = 'ectools_ctl_office';

    public function __construct($app)
    {
        parent::__construct($app);
        header("cache-control: no-store, no-cache, must-revalidate");
    }

    function index(){
        $this->showarea();
        /*
        $this->finder('ectools_mdl_office',array(
            'title'=>'办事处管理',
            'actions'=>array(array(
                'label'=>'安装新支付接口',
                'target'=>'_blank',
            ))));*/
    }

    /**
     * 展示所有办事处
     * @params null
     * @return null
     */
    private function showarea()
    {
        //$dArea = $this->app->model('office');
        $obj_office_op = kernel::service('ectools_office_apps', array('content_path'=>'ectools_office_operation'));
        $this->path[]=array('text'=>app::get('ectools')->_('办事处列表'));

        if ($obj_office_op->getTreeSize())
        {
            //超过100条
            $this->pagedata['area'] = $obj_office_op->getOfficeById();
            $this->page('delivery/office_area_treeList.html');
        }
        else
        {
            $obj_office_op->getMap();
            $this->pagedata['area'] = $obj_office_op->office;
            $this->page('delivery/office_area_map.html');
        }
    }

    public function showOfficeTreeList($serid,$multi=false)
    {
         if ($serid)
         {
            $this->pagedata['sid'] = $serid;
         }
         else
         {
            $this->pagedata['sid'] = substr(time(),6,4);
         }

         $this->pagedata['multi'] =  $multi;
         $this->singlepage('delivery/officeSelect.html');
    }

    public function getChildNode()
    {
        //$dArea = $this->app->model('office');
        $obj_office_op = kernel::service('ectools_office_apps', array('content_path'=>'ectools_office_operation'));
        $this->pagedata['area'] = $obj_office_op->getOfficeById($_POST['officeId']);
        $this->display('delivery/office_area_sub_treeList.html');
    }

    public function getOfficeById($pofficeid)
    {
        //$oDlyType = $this->app->model('office');
        $obj_office_op = kernel::service('ectools_office_apps', array('content_path'=>'ectools_office_operation'));
        echo json_encode($obj_office_op->getOfficeById($pofficeid));
    }

    /**
     * 添加新办事处界面
     * @params string 父级office id
     * @return null
     */
    public function showNewArea($pOfficeId=null)
    {
        if ($pOfficeId){
            $dArea = $this->app->model('office');
            $this->pagedata['parent'] = $dArea->getOfficeByParentId($pOfficeId);
        }
        $this->path[] = array('text'=>app::get('ectools')->_('添加配送办事处'));
        $this->display('delivery/office_area_new.html');
    }

    /**
     * 添加新办事处
     * @params null
     * @return null
     */
    public function addDlArea()
    {
        $this->begin('index.php?app=ectools&ctl=office&act=index');
        $obj_office_op = kernel::service('ectools_office_apps', array('content_path'=>'ectools_office_operation'));
        if(!$obj_office_op->insertDlArea($_POST,$msg)){
            $this->end(false, app::get('ectools')->_('新建失败，').$msg);
        }else
            $this->end(true, app::get('ectools')->_('新建成功，名称：').$_POST['local_name']);

    }

    /**
     * 修改办事处信息的入口
     * @params null
     * @return null
     */
    public function saveDlArea()
    {
        $this->begin('index.php?app=ectools&ctl=office&act=index');
        $obj_office_op = kernel::service('ectools_office_apps', array('content_path'=>'ectools_office_operation'));
        if(!$obj_office_op->updateDlArea($_POST,$msg)){
            $this->end(false, app::get('ectools')->_('修改失败，').$msg);
        }
        else
        {
            $this->end(true, app::get('ectools')->_('修改成功，名称：').$_POST['local_name']);
        }
    }

    /**
     * 编辑显示页面
     * @params string 办事处的office id
     * @return null
     */
    public function detailDlArea($aOfficeId)
    {
        $this->path[] = array('text'=>app::get('ectools')->_('配送办事处编辑'));
        $oObj = $this->app->model('office');
        $this->pagedata['area'] = $oObj->getDlAreaById($aOfficeId);
        $this->display('delivery/office_area_edit.html');
    }
//设置办事处默认值
   public  function defaultLocal($arg0){
        $dl_mdl = app::get('ectools')->model('office');
        $filter = array('default_local'=> 1);
        $saveData['default_local'] = 0;
        $res = $dl_mdl->update($saveData,$filter);
        $default_local = $dl_mdl->getList('*', array('office_id'=>$_GET['arg0']));
        $office_id = array('office_id' => $default_local[0]['office_id']);
        $saveData['default_local'] = 1;
        $ress = $dl_mdl->update($saveData,$office_id);
    }

    /**
     * 删除对应office id 的办事处
     * @params string office id
     * @return null
     */
    public function toRemoveArea($officeId)
    {
        $this->begin('index.php?app=ectools&ctl=office&act=index');
        $obj_office_op = kernel::service('ectools_office_apps', array('content_path'=>'ectools_office_operation'));
        if ($obj_office_op->toRemoveArea($officeId))
            $this->end(true,app::get('ectools')->_('删除成功！'));
        else
            $this->end(false,app::get('ectools')->_('删除失败！'));
    }

    /**
     * 更新地区排序数据
     * @params null
     * @return null
     */
    public function updateOrderNum()
    {
        $this->begin('index.php?app=ectools&ctl=office&act=index');

        $is_update = true;
        $dArea = $this->app->model('office');
        $arrPOdr = $_POST['p_order'];
        $arrAmount = $_POST['p_amount'];

        $arrOffice = array();
 //修改最低金额limited_amount值保存
       foreach ($arrAmount as $key=>$strAmount)
            {
                $arrdAreas = $dArea->dump($key, 'office_id,limited_amount');
                $arrdAreas['limited_amount'] = $strAmount ? $strAmount : '0';
                $arrOffice[] = $arrdAreas;
            }
        if ($arrPOdr)
        {
            foreach ($arrPOdr as $key=>$strPOdr)
            {
                $arrdArea = $dArea->dump($key, 'office_id,ordernum');
                $arrdArea['ordernum'] = $strPOdr ? $strPOdr : '50';
                $arrOffice[] = $arrdArea;
            }
        }
        if ($arrOffice)
        {
            foreach ($arrOffice as $arrOfficeinfo)
            {
                $is_update = $dArea->save($arrOfficeinfo);
            }
        }

        if($is_update) {
            $is_update = kernel::single('ectools_office_operation')->updateOfficeData();
        }
        
        $this->end($is_update,app::get('ectools')->_('排序成功！'));
    }

    public function selOffice()
    {
        $path = $_GET['path'];
        $depth = $_GET['depth'];

        header('Content-type: text/html;charset=utf8');
        //$local = $this->app->model('office');
        $local = kernel::single('ectools_office_select');
        $ret = $local->get_area_select($this->app,$path,array('depth'=>$depth));

        if($ret)
        {
            echo '&nbsp;-&nbsp;'.$ret;
        }
        else
        {
            echo '';
        }
    }
}
